#pragma once

#include"common.hpp"
#include"Socket.hpp"
#include "Toolhttp.hpp"


const std::string  wwwroot = "./wwwroot";
const std::string  index_my = "/index.html";
const std::string  g_404 = "/404.html";

class RequstHttp{
    public:
    RequstHttp(){}
    std::string Serialize()
    {return nullptr;}

    bool Deserialize(std::string& reqstr)
    {
        std::string line;
        bool n = ToolHttp::Readoneline(reqstr,&line);
        if(n > 0){
            std::stringstream ss(line);
            ss>>_method>>_uri>>_version;
        }

        if(_uri == "/"){
            _uri = wwwroot.c_str() + index_my;
            LOG(level::DEBUG)<<_uri;
        }
        else{
            _uri = wwwroot + _uri;
            LOG(level::DEBUG)<<_uri;
        }

        return true;       
    }

    std::string Uri(){
        return _uri;
    }

    ~RequstHttp(){}
    private:
    std::string _method;
    std::string _uri;
    std::string _version;
    std::unordered_map<std::string, std::string> _headers;

    std::string _text;
};

class RespondHttp{
    public:
    RespondHttp():_blankline(line_break)
    {}

    std::string Serialize()
    {
        std::string headers;
        headers += _version + gspace + std::to_string(_code) + gspace + _desc + line_break;
        for(auto &h : _headermap){
            headers += h.first + gsep + h.second + line_break;
        }
        return headers + _blankline + _text;
    }

    bool MakeRespond(std::string uri)
    {
        _version = "HTTP/1.1";
        _code = 200;
        _desc = "SUCCSS";

        if(!ToolHttp::GetfileContent(uri,&_text)){
            LOG(level::ERROR)<<"GetfileContent false or no resource to 404";
            _code = 404;
            _desc = "NO Found";
            std::string file404 = wwwroot + g_404;
            ToolHttp::GetfileContent(file404,&_text);
            return true;
        }
        else
            return true;
    }

    void Deserialize(std::string& str)
    {

    }

    ~RespondHttp(){}
    private:
    std::string _desc;
    int _code;
    std::string _version;
    std::unordered_map<std::string, std::string> _headermap;
    std::string _blankline;
    std::string _text;

    std::string _target;
};

class Http{
    public:
    Http()
    {}
    bool Decode()
    {
        return true;
    }

    void GetRequstHttp(std::shared_ptr<TcpSocket> tcpser){
        std::string rstr;
            int n = tcpser->Recv(&rstr);
            if(n > 0){
                // std::cout<<rstr<<std::endl;
                RequstHttp req;
                req.Deserialize(rstr);

                RespondHttp resp;
                resp.MakeRespond(req.Uri());

                std::string message = resp.Serialize();
                tcpser->Send(message);
            
            }
            else if(n == 0){
                LOG(level::DEBUG)<<"exit...";
                exit(SUCCESS);
            }
            else{
                LOG(level::ERROR)<<"Unusual..";
                exit(READ_ERR);
            }


        
    }

    ~Http(){}

    private:
        // std::shared_ptr<TcpSocket> _tcpser;
};